package handlers

import (
	"code/config"
	"code/utils"
	"fmt"
	"strings"
)

// AddHandler 处理根据给定配置生成添加代码的逻辑
func AddHandler(cfg config.Config) config.CodeResult {
	var codes config.CodeResult
	blue.Printf("开始生成 %s 的代码\n", cfg.Action)

	codeController := controllerBuildAdd(cfg)
	codeControllerContent := fmt.Sprintf("//%s\n%s\n//%s\r\n\n", cfg.StartTag, codeController[0], cfg.EndTag)

	codes.ControllerCode = codeControllerContent
	codes.ServiceCode = fmt.Sprintf("//%s\n%s\n//%s\r\n\n", cfg.StartTag, serviceBuildAdd(cfg)[0], cfg.EndTag)
	codes.ServiceImplCode = fmt.Sprintf("//%s\n%s\n//%s\r\n\n", cfg.StartTag, serviceImplBuildAdd(cfg)[0], cfg.EndTag)
	codes.MapperCode = fmt.Sprintf("//%s\n%s\n//%s\r\n\n", cfg.StartTag, mapperBuildAdd(cfg)[0], cfg.EndTag)
	codes.MapperXMLCode = fmt.Sprintf("<!--%s-->\n%s\n<!--%s-->\r\n\n", cfg.StartTag, mapperXMLBuildAdd(cfg)[0], cfg.EndTag)

	green.Printf("生成完毕\n%s\n", cfg.Name)
	return codes
}

// 控制器构建函数 - 添加
func controllerBuildAdd(config config.Config) []string {
	controllerTemplate := `
// 控制器
@{ApiType}Mapping("{Api}")
public AjaxResult add{tag}(@RequestBody {Name} {nameVariable}) {
    return toAjax({service}.insert{Name}{tag}({nameVariable}));
}
`
	controllerCode := strings.ReplaceAll(controllerTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	controllerCode = strings.ReplaceAll(controllerCode, "{tag}", config.Tag)
	controllerCode = strings.ReplaceAll(controllerCode, "{nameVariable}", utils.ToCamelCase(config.Table))
	controllerCode = strings.ReplaceAll(controllerCode, "{service}", utils.ToCamelCase(config.Table)+"Service")
	controllerCode = strings.ReplaceAll(controllerCode, "{ApiType}", utils.ToPascalCase(config.ApiType))
	controllerCode = strings.ReplaceAll(controllerCode, "{Api}", config.Api)

	return []string{controllerCode}
}

// 服务构建函数 - 添加
func serviceBuildAdd(config config.Config) []string {
	serviceTemplate := `	
/**
 * 新增 {Name} 服务
 * 
 * @param {Name} {nameVariable}
 * @return 影响行数
 */
public int insert{Name}{tag}({Name} {nameVariable});
`
	serviceCode := strings.ReplaceAll(serviceTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	serviceCode = strings.ReplaceAll(serviceCode, "{tag}", config.Tag)
	serviceCode = strings.ReplaceAll(serviceCode, "{nameVariable}", utils.ToCamelCase(config.Table))

	return []string{serviceCode}
}

// 服务实现构建函数 - 添加
func serviceImplBuildAdd(config config.Config) []string {
	serviceImplTemplate := `
// {Name}服务实现	
@Override
public int insert{Name}{tag}({Name} {nameVariable}) {
	{nameVariable}.setCreateTime(DateUtils.getNowDate());
    return {mapper}.insert{Name}{tag}({nameVariable});
}
`
	serviceImplCode := strings.ReplaceAll(serviceImplTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	serviceImplCode = strings.ReplaceAll(serviceImplCode, "{tag}", config.Tag)
	serviceImplCode = strings.ReplaceAll(serviceImplCode, "{nameVariable}", utils.ToCamelCase(config.Table))
	serviceImplCode = strings.ReplaceAll(serviceImplCode, "{mapper}", utils.ToCamelCase(config.Table)+"Mapper")

	return []string{serviceImplCode}
}

// Mapper构建函数 - 添加
func mapperBuildAdd(config config.Config) []string {
	mapperTemplate := `
// Mapper
/**
 * 新增 {Name}
 * 
 * @param {Name} {nameVariable}
 * @return 影响行数
 */
public int insert{Name}{tag}({Name} {nameVariable});
`
	mapperCode := strings.ReplaceAll(mapperTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	mapperCode = strings.ReplaceAll(mapperCode, "{tag}", config.Tag)
	mapperCode = strings.ReplaceAll(mapperCode, "{nameVariable}", utils.ToCamelCase(config.Table))

	return []string{mapperCode}
}

// Mapper XML构建函数 - 添加
func mapperXMLBuildAdd(config config.Config) []string {
	mapperXMLTemplate := `
<insert id="insert{Name}{tag}" parameterType="{Name}" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO {table}
	<trim prefix="(" suffix=")" suffixOverrides=",">
	<if test="sort != null">sort,</if>
	<if test="createTime != null">create_time,</if>
	<if test="isEnable != null">is_enable,</if>
	<if test="title != null and title != ''">title,</if>
	<if test="displayTime != null">display_time,</if>
	<if test="cover != null">cover,</if>
	<if test="content != null">content,</if>
	<if test="info != null">info,</if>
	<if test="keyword != null">keyword,</if>
	<if test="copyfrom != null">copyfrom,</if>
	<if test="focus != null">focus,</if>
	<if test="sug != null">sug,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
	<if test="sort != null">#{sort},</if>
	<if test="createTime != null">#{createTime},</if>
	<if test="isEnable != null">#{isEnable},</if>
	<if test="title != null and title != ''">#{title},</if>
	<if test="displayTime != null">#{displayTime},</if>
	<if test="cover != null">#{cover},</if>
	<if test="content != null">#{content},</if>
	<if test="info != null">#{info},</if>
	<if test="keyword != null">#{keyword},</if>
	<if test="copyfrom != null">#{copyfrom},</if>
	<if test="focus != null">#{focus},</if>
	<if test="sug != null">#{sug},</if>
</trim>
</insert>
`
	mapperXMLCode := strings.ReplaceAll(mapperXMLTemplate, "{Name}", utils.LineToPascalCase(config.Table))
	mapperXMLCode = strings.ReplaceAll(mapperXMLCode, "{tag}", config.Tag)
	mapperXMLCode = strings.ReplaceAll(mapperXMLCode, "{table}", config.Table)

	return []string{mapperXMLCode}
}
